using PPIDataAccess.General;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Threading;

namespace PPIDataAccess.Data
{
	public static class DUtility_Asset_XMissionStructure
	{
		#region Query
        public static DataSet SelectAllIdName(string connectionKey)
		{
			SqlDataAdapter _Da = null;
			try
			{
                _Da = new SqlDataAdapter("up_Utility_Asset_XMissionStructures_SelectAllIdName", DBHelper.GetConnection(connectionKey));
				_Da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet _Ds = new DataSet();
				_Da.Fill(_Ds);
				return _Ds;
			}
			catch (Exception _Ex)
			{
				throw new Exception("Error executing up_Utility_Asset_XMissionStructures_SelectAllIdName", _Ex);
			}
			finally
			{
				if (_Da != null) _Da.SelectCommand.Connection.Close();
			}
		}

        public static DataSet SelectUtilityNearVendor(Int32 vendorID, Int32 maxDistance, string connectionKey)
        {
            SqlDataAdapter _Da = null;
            try
            {
                _Da = new SqlDataAdapter("up_Utility_Asset_XMissionStructures_NearByVendor", DBHelper.GetConnection(connectionKey));
                _Da.SelectCommand.CommandType = CommandType.StoredProcedure;
                _Da.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@vendorId", ParameterDirection.Input, false, vendorID));
                _Da.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@max_distance", ParameterDirection.Input, false, maxDistance));
                DataSet _Ds = new DataSet();
                _Da.Fill(_Ds);
                return _Ds;
            }
            catch (Exception _Ex)
            {
                throw new Exception("Error executing up_Utility_Asset_XMissionStructures_NearByVendor", _Ex);
            }
            finally
            {
                if (_Da != null) _Da.SelectCommand.Connection.Close();
            }
        }

        public static DataSet SelectAllServer(string connectionKey)
        {
            SqlDataAdapter _Da = null;
            try
            {
                _Da = new SqlDataAdapter("up_Utility_Asset_XMissionStructures_SelectAllServer", DBHelper.GetConnection(connectionKey));
                _Da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet _Ds = new DataSet();
                _Da.Fill(_Ds);
                return _Ds;
            }
            catch (Exception _Ex)
            {
                throw new Exception("Error executing up_Utility_Asset_XMissionStructures_SelectAllServer", _Ex);
            }
            finally
            {
                if (_Da != null) _Da.SelectCommand.Connection.Close();
            }
        }

        public static DataSet SelectAllServer(int utilityID, string connectionKey)
        {
            SqlDataAdapter _Da = null;
            try
            {
                _Da = new SqlDataAdapter("up_Utility_Asset_XMissionStructures_SelectAllServerByUtility", DBHelper.GetConnection(connectionKey));
                _Da.SelectCommand.CommandType = CommandType.StoredProcedure;
                _Da.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@utilityId", ParameterDirection.Input, false, utilityID));
                DataSet _Ds = new DataSet();
                _Da.Fill(_Ds);
                return _Ds;
            }
            catch (Exception _Ex)
            {
                throw new Exception("Error executing up_Utility_Asset_XMissionStructures_SelectAllServerByUtility", _Ex);
            }
            finally
            {
                if (_Da != null) _Da.SelectCommand.Connection.Close();
            }
        }

        public static DataSet SelectAllByQueryAsset(int queryAssetID, string connectionKey)
        {
            SqlDataAdapter _Da = null;
            try
            {
                _Da = new SqlDataAdapter("up_Utility_Asset_XMissionStructures_SelectAllServerByQueryAsset", DBHelper.GetConnection(connectionKey));
                _Da.SelectCommand.CommandType = CommandType.StoredProcedure;
                _Da.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@QueryAssetFK", ParameterDirection.Input, false, queryAssetID));
                DataSet _Ds = new DataSet();
                _Da.Fill(_Ds);
                return _Ds;
            }
            catch (Exception _Ex)
            {
                throw new Exception("Error executing up_Utility_Asset_XMissionStructures_SelectAllServerByQueryAsset", _Ex);
            }
            finally
            {
                if (_Da != null) _Da.SelectCommand.Connection.Close();
            }
        }
		#endregion Query

		#region CRUD
        public static void NewByVendor(int vendorId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Utility_Asset_XMissionStructures_DuplicateByVendor", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@vendorId", ParameterDirection.Input, false, vendorId));
                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void Update(Int32 structureID, Int32 projectID, Int32 XMissionStructures_TopTypeID, double Latitude, double Logitude, double Alititude, Int32 SatelliteCount, DateTime SatelliteTime, bool SolutionAvaible, int Handheld, Int32 contactID, DateTime date, int countImages, int utilityFK, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Utility_Asset_XMissionStructures_Update", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@utilityID", ParameterDirection.Input, false, structureID));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@UtilityFK", ParameterDirection.Input, false, utilityFK));

                cmd.Parameters.Add(DBHelper.CreateBitParameter("@SolutionAvailable", ParameterDirection.Input, false, SolutionAvaible));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@CountImages", ParameterDirection.Input, false, countImages));

                #region Utility_ProjectFK
                if (projectID == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Utility_ProjectFK", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Utility_ProjectFK", ParameterDirection.Input, false, projectID));
                }
                #endregion

                #region Utility_ContactFK
                if (contactID == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Utility_ContactFK", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Utility_ContactFK", ParameterDirection.Input, false, contactID));
                }
                #endregion

                #region XMissionStructure_TopTypeID
                if (XMissionStructures_TopTypeID == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@XMissionStructures_TopTypeFk", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@XMissionStructures_TopTypeFk", ParameterDirection.Input, false, XMissionStructures_TopTypeID));

                }
                #endregion

                #region Latitude
                if (Latitude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, false, Latitude));
                }
                #endregion

                #region Longitude
                if (Logitude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, true, DBNull.Value));

                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, false, Logitude));

                }
                #endregion

                #region Altitude
                if (Alititude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, true, DBNull.Value));

                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, false, Alititude));

                }
                #endregion

                #region SatelliteCount

                if (SatelliteCount == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, false, SatelliteCount));
                }



                #endregion

                #region SatelliteTime
                if (SatelliteTime == DateTime.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, true, SatelliteTime));
                }

                #endregion

                #region Handheld
                if (Handheld == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@Handheld", 255, ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Handheld", ParameterDirection.Input, false, Handheld));

                }
                #endregion


                #region Date
                if (date == DateTime.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@Date", ParameterDirection.Input, true, DBNull.Value));

                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@Date", ParameterDirection.Input, false, date));

                }
                #endregion

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void Delete(int utilityId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Utility_Asset_XMissionStructures_Delete", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@utilityID", ParameterDirection.Input, false, utilityId));
                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
                throw new Exception("Error executing up_Utility_Asset_XMissionStructures_Delete", ex);
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

		#endregion CRUD
	}
}

